home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PINBSRC.ZIP / SOUNDKIT.BAK < prev    next >
Text File  |  1995-03-27  |  3KB  |  146 lines

  1. {SOUNDKIT Ver. 1.0}
  2. Unit SOUNDKIT;
  3.  
  4. Interface
  5.  
  6. { ResetDSP returns true if reset was successful
  7.   base should be 1 for base address 210h, 2 for 220h etc... }
  8. function ResetDSP(base : word) : boolean;
  9.  
  10. { Write DAC sets the speaker output level }
  11. procedure WriteDAC(level : byte);
  12.  
  13. { ReadDAC reads the microphone input level }
  14. function ReadDAC : byte;
  15.  
  16. { SpeakerOn connects the DAC to the speaker }
  17. function SpeakerOn: byte;
  18.  
  19. { SpeakerOff disconnects the DAC from the speaker,
  20.   but does not affect the DAC operation }
  21. function SpeakerOff: byte;
  22.  
  23. { Functions to pause DMA playback }
  24. procedure DMAStop;
  25. procedure DMAContinue;
  26.  
  27. { Playback plays a sample of a given size back at a given frequency using
  28.   DMA channel 1. The sample must not cross a page boundry }
  29. procedure Playback(sound : Pointer; size : word; frequency : word);
  30.  
  31.  
  32. Implementation
  33.  
  34. Uses Crt;
  35.  
  36. var      DSP_RESET : word;
  37.      DSP_READ_DATA : word;
  38.     DSP_WRITE_DATA : word;
  39.   DSP_WRITE_STATUS : word;
  40.     DSP_DATA_AVAIL : word;
  41.  
  42. function ResetDSP(base : word) : boolean;
  43. begin
  44.  
  45.   base := base * $10;
  46.  
  47.   { Calculate the port addresses }
  48.   DSP_RESET := base + $206;
  49.   DSP_READ_DATA := base + $20A;
  50.   DSP_WRITE_DATA := base + $20C;
  51.   DSP_WRITE_STATUS := base + $20C;
  52.   DSP_DATA_AVAIL := base + $20E;
  53.  
  54.   { Reset the DSP, and give some nice long delays just to be safe }
  55.   Port[DSP_RESET] := 1;
  56.   Delay(10);
  57.   Port[DSP_RESET] := 0;
  58.   Delay(10);
  59.   if (Port[DSP_DATA_AVAIL] And $80 = $80) And
  60.      (Port[DSP_READ_DATA] = $AA) then
  61.     ResetDSP := true
  62.   else
  63.     ResetDSP := false;
  64. end;
  65.  
  66. procedure WriteDSP(value : byte);
  67. begin
  68.   while Port[DSP_WRITE_STATUS] And $80 <> 0 do;
  69.   Port[DSP_WRITE_DATA] := value;
  70. end;
  71.  
  72. function ReadDSP : byte;
  73. begin
  74.   while Port[DSP_DATA_AVAIL] and $80 = 0 do;
  75.   ReadDSP := Port[DSP_READ_DATA];
  76. end;
  77.  
  78. procedure WriteDAC(level : byte);
  79. begin
  80.   WriteDSP($10);
  81.   WriteDSP(level);
  82. end;
  83.  
  84. function ReadDAC : byte;
  85. begin
  86.   WriteDSP($20);
  87.   ReadDAC := ReadDSP;
  88. end;
  89.  
  90. function SpeakerOn: byte;
  91. begin
  92.   WriteDSP($D1);
  93. end;
  94.  
  95. function SpeakerOff: byte;
  96. begin
  97.   WriteDSP($D3);
  98. end;
  99.  
  100. procedure DMAContinue;
  101. begin
  102.   WriteDSP($D4);
  103. end;
  104.  
  105. procedure DMAStop;
  106. begin
  107.   WriteDSP($D0);
  108. end;
  109.  
  110. procedure Playback(sound : Pointer; size : word; frequency : word);
  111. var time_constant : word;
  112.      page, offset : word;
  113. begin
  114.  
  115. {  WriteDSP($D1); {SpeakerOn}
  116.  
  117.   size := size - 1;
  118.  
  119.   { Set up the DMA chip }
  120.   offset := Seg(sound^) Shl 4 + Ofs(sound^);
  121.   page := (Seg(sound^) + Ofs(sound^) shr 4) shr 12;
  122. {  writeln(Seg(sound^),':',ofs(sound^), 'Page:',page,' Offset:',offset);}
  123.   if size>65536-offset then size:=65536-offset;
  124. {  writeln('SIZE:',size);                                                 }
  125.   Port[$0A] := 5;
  126.   Port[$0C] := 0;
  127.   Port[$0B] := $49;
  128.   Port[$02] := Lo(offset);
  129.   Port[$02] := Hi(offset);
  130.   Port[$83] := page;
  131.   Port[$03] := Lo(size);
  132.   Port[$03] := Hi(size);
  133.   Port[$0A] := 1;
  134.  
  135.   { Set the playback frequency }
  136.   time_constant := 256 - 1000000 div frequency;
  137.   WriteDSP($40);
  138.   WriteDSP(time_constant);
  139.  
  140.   { Set the playback type (8-bit) }
  141.   WriteDSP($14);
  142.   WriteDSP(Lo(size));
  143.   WriteDSP(Hi(size));
  144. end;
  145. end.
  146.